package com.shouer.doc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.shouer.doc.R;

/**
 * Created by ZXL on 2016/12/27.
 */

public class RemoteLoginDialog extends Dialog implements DialogInterface.OnKeyListener{

    private View mDialogView;
    private TextView messageView, centerBtn;
    private DialogDismissListener dismissListener;


    public RemoteLoginDialog(Context context) {
        super(context);
        init(context);
    }

    public RemoteLoginDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected RemoteLoginDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.gravity= Gravity.CENTER_HORIZONTAL|Gravity.TOP;
        getWindow().setAttributes((WindowManager.LayoutParams) params);
    }

    private void init(Context context) {

        mDialogView = View.inflate(context, R.layout.dialog_remotelogin_layout, null);
        messageView = (TextView) mDialogView.findViewById(R.id.message);
        centerBtn = (TextView) mDialogView.findViewById(R.id.remotelogin_center_btn);
        setContentView(mDialogView);
        setCanceledOnTouchOutside(false);
        setOnKeyListener(this);
    }

    public void withBtnClickListener(View.OnClickListener clickListener) {
        centerBtn.setOnClickListener(clickListener);
        centerBtn.setTag(R.id.dialog_btn_tag, 0);
    }


    public void withMessage(String message) {
        messageView.setText(message);
        messageView.setVisibility(View.VISIBLE);

    }



    public void withDialogDismissListener(DialogDismissListener dismissListener) {
        this.dismissListener = dismissListener;
    }

    @Override
    public void dismiss()
    {
        super.dismiss();
        if (null!=dismissListener)
        {
            dismissListener.onDialogDismiss();
        }
    }

    @Override
    public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
        if (keyCode==KeyEvent.KEYCODE_BACK)
        {
            return true;
        }
        return false;
    }
}
